package com.tanhua.server.controller;

import com.tanhua.server.pojo.Answers;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.ConclusionVo;
import com.tanhua.server.vo.PaperListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author YuShiLong
 * @Date 2021/4/27 14:46
 * @Version 1.0
 */
@RestController
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    @GetMapping
    public ResponseEntity<List<PaperListVo>> paperList() {
        try {
            List<PaperListVo> list = this.testSoulService.queryPaperList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/report/{id}")
    public ResponseEntity<ConclusionVo> getReport(@PathVariable("id") String id) {
        //调业务层
        try {
            ConclusionVo conclusionVo = testSoulService.getReport(id);
            if (null != conclusionVo) {
                return ResponseEntity.ok(conclusionVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /* 提交问卷
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity<String> submitTestPaper(@RequestBody Map<String, List<Answers>> map){

        try {
            //返回报告ID
            Collection<List<Answers>> values = map.values();

           /* for (List<Answers> value : values) {
                System.out.println(value);
            }*/
            String reportId=this.testSoulService.submitTestPaper(map);
            //如果不为空就成功
            if(StringUtils.isNotEmpty(reportId)){
                return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
